// pages/editInf/editInf.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickerc:'#acacac',
    pickerc2:'#acacac',
    pickerc3:'#acacac',
    array: ['未选择','男', '女', '保密'],
    array2: ['未选择','大学城校区', '五山校区'],
    array3: ['未选择','2020级', '2019级', '2018级', '2017级', '2016级'],
    gender:0,
    campus:0,
    grade:0,
    hobby:""
  },


  bindPickerChange: function(e) {
    this.setData({
      gender: e.detail.value
    })
    if(e.detail.value==0){
      this.setData({
        pickerc:'#acacac',
      })
    }
    else{
      this.setData({
        pickerc:'#000000',
      })
    }
  },//选择分区

  bindPickerChange2: function(e) {
    console.log(e.detail.value)
    this.setData({
      campus: e.detail.value
    })
    if(e.detail.value==0){
      this.setData({
        pickerc2:'#acacac',
      })
    }
    else{
      this.setData({
        pickerc2:'#000000',
      })
    }
  },

  bindPickerChange3: function(e) {
    this.setData({
      grade: e.detail.value
    })
    if(e.detail.value==0){
      this.setData({
        pickerc3:'#acacac',
      })
    }
    else{
      this.setData({
        pickerc3:'#000000',
      })
    }
  },

  resetdata(){
    this.setData({
      gender:0,
      campus:0,
      grade:0,
      hobby:"",
      pickerc:'#acacac',
      pickerc2:'#acacac',
      pickerc3:'#acacac',
    })
  },

  sethobby(e){
    this.setData({
       hobby:e.detail.value
    })
    console.log(this.data.hobby)
  },//获取标题

  updata(){
    wx.showLoading({
      title: '保存中...',
      mask:true
    })

    wx.cloud.callFunction({
      name:"updateUserInfo",
      data:{
        select:0,
        openid:app.globalData.openid,
        gender:this.data.gender,
        campus:this.data.campus,
        grade:this.data.grade,
        hobby:this.data.hobby
      },
      success(res){
        console.log(res)
        console.log("数据更新成功",res)
        wx.hideLoading()
        wx.showToast({
          title: '保存成功！',
          icon: 'success',
          duration: 1500
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        }, 2000)
      },
      fail(res){
       console.log("数据更新失败",res)
      }
    })
  },

  load_data(){
    let that=this
    wx.cloud.callFunction({
      name:"getUserInfo",
      data:{
        openid:app.globalData.openid
      },
      success(res){
         console.log(res)
         that.setData({
           gender:res.result.data['0']["gender"],
           campus:res.result.data['0']["campus"],
           grade:res.result.data['0']["grade"],
           hobby:res.result.data['0']["hobby"],
         })
         
      },
      fail(res){
       console.log("数据拉取失败",res)
      }
    })
  },

  onLoad: function (options) {
    this.load_data()
  }

})